package com.qikecn.api.model;

import com.qikecn.utils.NullUtil;
import com.qikecn.utils.SpyMemcachedUtil;

import java.io.Serializable;

/**
 * 内存数据库缓存
 * @author wangst
 */
public class MemcachedUser implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7913953676559790758L;
	
	private static final int expire = 3600 * 48;
	
	/**
	 * 保存用户信息到内存数据库
	 * @param key
	 * @param user
	 * @return
	 */
	public static boolean setMemcachedUser(String key, User user){
		boolean flag = false;
		if(SpyMemcachedUtil.keyExists("key_"+user.getStr("id"))){//禁止同时登录
			String keyTemp = SpyMemcachedUtil.get("key_"+user.getStr("id")).toString();
			deleteMemcachedUser(keyTemp);
		}
		//保存登录信息
		SpyMemcachedUtil.set("key_"+user.getStr("id"), key, expire);
		SpyMemcachedUtil.set(key + "_id", user.get("id"), expire);
		SpyMemcachedUtil.set(key + "_email", NullUtil.ifNull(user.get("email")), expire);
		SpyMemcachedUtil.set(key + "_phone", NullUtil.ifNull(user.get("phone")), expire);
		SpyMemcachedUtil.set(key + "_username", NullUtil.ifNull(user.get("username")), expire);
		SpyMemcachedUtil.set(key + "_qq", NullUtil.ifNull(user.get("qq")), expire);
		SpyMemcachedUtil.set(key + "_truename", NullUtil.ifNull(user.get("truename")), expire);
		SpyMemcachedUtil.set(key + "_zongji", NullUtil.ifNull(user.get("zongji")), expire);
		SpyMemcachedUtil.set(key + "_usertype", NullUtil.ifNull(user.getInt("usertype")+""), expire);
		SpyMemcachedUtil.set(key + "_clienttype", NullUtil.ifNull(user.get("clienttype")), expire);
		return flag;
	}

	/**
	 * 从内容数据库获取用户信息
	 * @param key
	 * @return
	 */
	public static MemcachedUser getMemcachedUser(String key){
		MemcachedUser memcachedUser = null;
		if(SpyMemcachedUtil.keyExists(key+"_id")){
			memcachedUser = new MemcachedUser();
			memcachedUser.setKey(key);
			memcachedUser.setId(NullUtil.ifNull(SpyMemcachedUtil.get(key + "_id")));
			memcachedUser.setEmail(NullUtil.ifNull(SpyMemcachedUtil.get(key + "_email")));
			memcachedUser.setPhone(NullUtil.ifNull(SpyMemcachedUtil.get(key + "_phone")));
			memcachedUser.setUsername(NullUtil.ifNull(SpyMemcachedUtil.get(key + "_username")));
			memcachedUser.setQq(NullUtil.ifNull(SpyMemcachedUtil.get(key + "_qq")));
			memcachedUser.setTruename(NullUtil.ifNull(SpyMemcachedUtil.get(key + "_truename")));
			memcachedUser.setZongji(NullUtil.ifNull(SpyMemcachedUtil.get(key + "_zongji")));
			memcachedUser.setUsertype( Integer.parseInt(NullUtil.ifNull(SpyMemcachedUtil.get(key + "_usertype"))) );
			memcachedUser.setClienttype(NullUtil.ifNull(SpyMemcachedUtil.get(key + "_clienttype")));
			memcachedUser.setUid(NullUtil.ifNull(SpyMemcachedUtil.get(key + "_uid")));
		}
		return memcachedUser;
	}
	
	/**
	 * 清除内存数据库信息
	 * @param key
	 */
	public static void deleteMemcachedUser(String key){
		if(SpyMemcachedUtil.keyExists(key+"_id")){
			SpyMemcachedUtil.delete( NullUtil.ifNull( SpyMemcachedUtil.get("key_"+ SpyMemcachedUtil.get(key + "_id")) ) );
			SpyMemcachedUtil.delete(key + "_id");
			SpyMemcachedUtil.delete(key + "_email");
			SpyMemcachedUtil.delete(key + "_phone");
			SpyMemcachedUtil.delete(key + "_username");
			SpyMemcachedUtil.delete(key + "_qq");
			SpyMemcachedUtil.delete(key + "_truename");
			SpyMemcachedUtil.delete(key + "_zongji");
			SpyMemcachedUtil.delete(key + "_usertype");
			SpyMemcachedUtil.delete(key + "_clienttype");
			SpyMemcachedUtil.delete(key + "_uid");
		}
	}
	
	private String key;
	private String id;
	private String email;
	private String phone;
	private String qq;
	private String truename;
	private String username;
	private String zongji;
	private int usertype;
	private String clienttype;
	private String uid;

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	public String getTruename() {
		return truename;
	}
	public void setTruename(String truename) {
		this.truename = truename;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}

	public String getZongji() {
		return zongji;
	}

	public void setZongji(String zongji) {
		this.zongji = zongji;
	}

	public int getUsertype() {
		return usertype;
	}

	public void setUsertype(int usertype) {
		this.usertype = usertype;
	}

	public String getClienttype() {
		return clienttype;
	}

	public void setClienttype(String clienttype) {
		this.clienttype = clienttype;
	}
	
}
